package com.qi.qipay.commons.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    public static String md5(String str) {
        if (!str.isEmpty()) {
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                byte[] bytes = str.getBytes();
                byte[] results=messageDigest.digest(bytes);
                StringBuilder stringBuilder=new StringBuilder();
                for(byte result:results){
                    stringBuilder.append(String.format("%02x",result));
                }
                return stringBuilder.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static String shortMd5(String str) {
        return md5(str).substring(8, 24);
    }

    public static String md5WithSalt(String str, String salt) {
        return md5(salt + str + salt);
    }
}
